package com.abc.robot01.activity.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.LocalIndication
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.indication
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidthIn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.GenericShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material.ripple.rememberRipple
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDefaults
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.zIndex
import com.abc.robot01.R
import com.abc.robot01.activity.px
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class DataPickerDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DatePickerDocked2()
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DatePickerModal(
    onDateSelected: (Long?) -> Unit={},
    onDismiss: () -> Unit ={}
) {
    val datePickerState = rememberDatePickerState()

    DatePickerDialog(
        onDismissRequest = onDismiss,
        confirmButton = {
            TextButton(onClick = {
                onDateSelected(datePickerState.selectedDateMillis)
                onDismiss()
            }) {
                Text("OK")
            }
        },
        dismissButton = {
            TextButton(onClick = onDismiss) {
                Text("Cancel")
            }
        }
    ) {
        BoxWithConstraints {
            // 360 is minimum because DatePicker uses 12.dp horizontal padding and 48.dp for each week day
            val scale = remember(this.maxWidth) { if(this.maxWidth > 360.dp) 0.6f else (this.maxWidth / 360.dp) }
            // Make sure there is always enough room, so use requiredWidthIn
            Box(modifier = Modifier.requiredWidthIn(min = 360.dp)) {
                // Scale in case the width is too large for the screen
                androidx.compose.material3.DatePicker(
                    modifier = Modifier.scale(0.6f),
                    state = datePickerState,
                    title = null,
                    headline = null,
                    showModeToggle = false,
                )
            }
        }


//        DatePicker(state = datePickerState,  modifier = Modifier.scale(0.4f))
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private  fun DatePickerDocked() {
    val datePickerState = rememberDatePickerState()
    Box(
        modifier = Modifier
            .fillMaxSize()
//            .offset(x = -616.px)
    ) {
                    DatePicker(
                        colors  = DatePickerDefaults.colors().copy(containerColor = Color.White),
                        modifier = Modifier.scale(0.45f).background(Color.White),
                        state = datePickerState,
                        showModeToggle = false
                    )
    }
}


@Composable
private fun DatePickerDocked2() {
//    val scrollState = rememberScrollState()
    val scrollState = ScrollState(489)
    Column(
        verticalArrangement = Arrangement.spacedBy(16.dp),
        modifier = Modifier
            .verticalScroll(scrollState)
            .fillMaxSize()
    ) {
//                    DatePickerModal()
        MaterialTheme {
            DatePickerDocked()
        }
    }
    Log.e("DataPickerDemo",""+scrollState.value)
}

fun convertMillisToDate(millis: Long): String {
    val formatter = SimpleDateFormat("MM/dd/yyyy", Locale.getDefault())
    return formatter.format(Date(millis))
}









